/*
 * SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: MulanPSL-2.0
 */
#![allow(non_upper_case_globals, unused_assignments, unused_mut)]
use crate::common::*;
extern "C" {
    fn strftime(
        __s: *mut libc::c_char,
        __maxsize: size_t,
        __format: *const libc::c_char,
        __tp: *const tm,
    ) -> size_t;
    fn localtime(__timer: *const time_t) -> *mut tm;
}

/*
 * Return a static buffer with the current date + time.
 */
#[no_mangle]
pub fn get_timestr(mut tstamp: time_t, mut log_year: libc::c_int) -> *mut libc::c_char {
    static mut buf: [libc::c_char; 128] = [0; 128];
    let mut timeptr: *mut tm = std::ptr::null_mut::<tm>();

    timeptr = unsafe { localtime(&tstamp) };
    unsafe {
        if !timeptr.is_null() {
            /* strftime() does not guarantee to NUL-terminate so we must check. */
            buf[(::core::mem::size_of::<[libc::c_char; 128]>() as libc::c_ulong)
                .wrapping_sub(1 as libc::c_int as libc::c_ulong) as usize] =
                '\0' as i32 as libc::c_char;
            if strftime(
                buf.as_mut_ptr(),
                ::core::mem::size_of::<[libc::c_char; 128]>() as libc::c_ulong,
                if log_year != 0 {
                    b"%h %e %T %Y\0" as *const u8 as *const libc::c_char
                } else {
                    b"%h %e %T\0" as *const u8 as *const libc::c_char
                },
                timeptr,
            ) != 0 as libc::c_int as libc::c_ulong
                && buf[(::core::mem::size_of::<[libc::c_char; 128]>() as libc::c_ulong)
                    .wrapping_sub(1 as libc::c_int as libc::c_ulong)
                    as usize] as libc::c_int
                    == '\0' as i32
            {
                return buf.as_mut_ptr();
            }
        }
    }
    std::ptr::null_mut::<libc::c_char>()
}
